import axios from 'axios';
import {aria2cPort} from "../config";

const ARIA2_RPC_URL = `http://localhost:${aria2cPort}/jsonrpc`;

const aria2Request = async (method, params = []) => {
	const id = new Date().getTime().toString();
	const requestData = {
		jsonrpc: '2.0',
		method: `aria2.${method}`,
		id: id,
		params: params,
	};
	// console.log("requestData",requestData)
	try {
		const response = await axios.post(ARIA2_RPC_URL, requestData);
		return response.data.result;
	} catch (error) {
		console.error(`Aria2 RPC request error: ${error}`);
		throw error;
	}
};

export const addUri = async (socket, jsonData) => {
	socket.send(JSON.stringify(jsonData))
};

export const tellStatus = async (gid) => {
	return await aria2Request('tellStatus', [gid]);
};

export const remove = async (gid) => {
	return await aria2Request('remove', [gid]);
};

// 暂停下载
export const pause = async (gid) => {
	return await aria2Request('pause', [gid]);
};
//恢复下载
export const unpause = async (gid) => {
	return await aria2Request('unpause', [gid]);
};


export const getGlobalStat = async () => {
	return await aria2Request('getGlobalStat');
};

export const getActiveDownloads = async () => {
	return await aria2Request('tellActive');
};